/*-----------------------------------------------File Info------------------------------------------------
** File Name:               serialFlash.c  
** Last modified date:      2016.07.22
** Last version:            V0.1
** Description:             
**--------------------------------------------------------------------------------------------------------            
** Created date:            2016.09.07
** author:                  YLY
** Version:                 V0.1
** Descriptions:            串行闪存器
**--------------------------------------------------------------------------------------------------------
*/


#ifndef _SERIAL_FLASH_H_
#define _SERIAL_FLASH_H_  

#include "userConfig.h"


//#define  FLASH_PRINTF			USERPRINTF
#define  FLASH_PRINTF(...)	

#define FLASH_SPI	SPI0

/* 厂商标识 */
#define FLASH_MANUFACTURER_SST	 		0xBF	
#define FLASH_MANUFACTURER_WINBOND	 	0xEF	
#define FLASH_MANUFACTURER_GD	 		0xC8
/* 芯片ID */
/* SST */
#define FLASH_CHIP_SST25VF040_ID	0x8D	
#define FLASH_CHIP_SST25VF080_ID	0x8E	
#define FLASH_CHIP_SST25VF016_ID  	0x41

/* 华邦WINBOND和DG的ID号相同 */
#define FLASH_CHIP_W25Q40_ID		0x12	
#define FLASH_CHIP_W25Q80_ID		0x13	
#define FLASH_CHIP_W25Q16_ID  		0x14

														
/* -------------------------------------------------------------------*/
#define ENABLE_MODIFY					0x00					/* 使能修改 */
#define DISABLE_MODIFY					0x1C					/* 禁止修改，数据保护 */
/* --------命令---------------------------------------------------------*/
#define	READ_SERIAL_FLASH				0x03					/* 25M普通方式读	 */					
#define	HIGHT_SPEED_READ_SERIAL_FLASH	0x0B					/* 80M高速读	 */

#define	ERASE_4K_SERIAL_FLASH			0x20					/* 4K扇区擦除 */
#define	ERASE_32K_SERIAL_FLASH			0x52					/* 32K块擦除 */
#define	ERASE_64K_SERIAL_FLASH			0xD8					/* 64K块擦除 */
#define	ERASE_CHIP_SERIAL_FLASH			0x60					/* 芯片擦除 */
#define	ERASE_CHIP_SERIAL_FLASH1		0xC7					/* 芯片擦除 */

#define	WRITE_BYTE_SERIAL_FLASH			0x02					/* 字节编程 */
#define	WRITE_AAI_SERIAL_FLASH			0xAD					/* 自动地址自增编程 */

#define RDSR_SERIAL_FLASH  				0x05					/* 读状态寄存器指令 */
#define EWSR_SERIAL_FLASH  				0x50					/* 使能写状态寄存器 */
#define WRSR_SERIAL_FLASH  				0x01					/* 写状态寄存器 */ 

#define WREN_SERIAL_FLASH  				0x06					/* 写使能 */
#define WRDI_SERIAL_FLASH  				0x04					/* 写禁止 */

#define RDID_SERIAL_FLASH  				0x90					/* 读ID */
#define JEDEC_ID_SERIAL_FLASH  			0x9F					/* 读JEDEC ID */

#define EBSY_SERIAL_FLASH  				0x70					/* 在AAI编程期间使能SO以输出RY/SY#状态 */
#define DBSY_SERIAL_FLASH  				0x80					/* 在AAI编程期间禁止SO以输出RY/SY#状态 */
/* -------------------------------------------------------- */


extern uint8_t ucgManufacturer;		/* 厂商 */
extern uint8_t ucgSerialFlashType;		/* 型号 */




/* ---------不常用通用函数-------------------------------- */
extern void vSerialFlashWaitBusy(void);
extern void vSerialFlashEwsr(void);	
extern void vSerialFlashWrsr(uint8_t byte); 
extern void vSerialFlashWren(void);  
extern void vSerialFlashWrdi(void);

/* 不常用通用函数 */
extern void vEnableSerialFlashReadByte(uint32_t uiAddress); 
extern void vDisableSerialFlashReadByte(void);

/* SST专用函数 */
extern void vSerialFlashAutoAddIncA(uint32_t uiAddress, uint8_t ucByte1,uint8_t ucByte2); 
extern void vSerialFlashAutoAddIncB(uint8_t ucByte1,uint8_t ucByte2);  	
extern void vSerialFlashAutoAddIncBuf(uint32_t uiAddress,uint8_t *pucDataBuf,uint8_t ucLen);

/* 非SST专用函数 */
extern void vSerialFlashWritePageBuf(uint32_t uiAddress,uint8_t *pucDataBuf,uint8_t ucLen);






/* ---------常用通用函数-------------------------------- */
extern void vSerialFlashInit(void);
extern void vSerialFlashReadId(uint8_t *pucManufacturer,uint8_t *pucId);
extern uint8_t ucSerialFlashReadStatusRegister(void);  

extern void vSerialFlashWriteByte(uint32_t uiAddress,uint8_t ucByte);
extern uint8_t ucSerialFlashReadByte(uint32_t uiAddress);  
extern void vSerialFlashWriteBuf(uint32_t uiAddress,uint8_t *pucDataBuf,uint8_t ucLen); 
extern void vSerialFlashReadBuf(uint32_t uiAddress,uint8_t *pucBuf,uint8_t ucLen);

extern void vSerialFlashChipErase(void);
extern void vSerialFlashSectorErease4K(uint32_t uiAddress);
extern void vSerialFlashBlockErease32K(uint32_t uiAddress);
extern void vSerialFlashBlockErease64K(uint32_t uiAddress);


#endif



